package com.woniuxy.service.impl;

import com.woniuxy.dao.CommentDao;
import com.woniuxy.dao.UsersDao;
import com.woniuxy.entity.Comment;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: 杜昊男  <br/>
 * Date: 2022/3/12:10:19  <br/>
 * Description:
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired(required = false)
    CommentDao commentDao;
    @Autowired(required = false)
    UsersDao usersDao;
    private Set<Comment> parentsList = new HashSet<>();
    @Override
    public ResponseEntity findComments(Comment comment) {
        List<Comment> commentList = commentDao.findComments(comment);
        return ResponseEntity.success("成功", findParent(commentList));
    }

    @Override
    public ResponseEntity createComments(Comment comment) {
         int res = commentDao.createComments(comment);
        findParents(comment);
        for (Comment parent:parentsList) {
            commentDao.commentNumChange(parent.getCommentid(), 1);
        }
//        int res = commentDao.createComments(comment);
//        if (res > 0) {
//            //获取评论的父id
//            Integer parentid = comment.getParentid();
//            if (parentid != null) {
//                commentDao.commentNumChange(parentid, 1);
//            }
//        }
        return ResponseEntity.success("评论成功");
    }

    @Override
    public ResponseEntity updateLike(int commentid, int isLiked) {
        int res = commentDao.updateLike(commentid, isLiked);
        if (res > 0) {
            return ResponseEntity.success("点赞成功");
        }
        return ResponseEntity.error("点赞失败");
    }

    public List<Comment> findParent(List<Comment> comments) {

        for (Comment comment : comments) {

            // 防止checkForComodification(),而建立一个新集合
            ArrayList<Comment> fatherChildren = new ArrayList<>();
            // 递归处理子级的回复，即回复内有回复
            findChildren(comment, fatherChildren);

            // 将递归处理后的集合排序再放回父级的孩子中
            Collections.sort(fatherChildren);
            comment.setReply(fatherChildren);
        }
        Collections.sort(comments);
        return comments;
    }

    public void findChildren(Comment parent, List<Comment> fatherChildren) {

        // 找出直接子级
        List<Comment> comments = parent.getReply();

        // 遍历直接子级的子级
        for (Comment comment : comments) {
            Comment parentComment = commentDao.findParentByCommentId(comment.getCommentid());
            if (parentComment!= null) {
                User user = usersDao.findUser(parentComment.getUserid());
                comment.setParentaccount(user.getAccount());
            }
            // 若非空，则还有子级，递归
            if (!comment.getReply().isEmpty()) {
                findChildren(comment, fatherChildren);
            }
            // 已经到了最底层的嵌套关系，将该回复放入新建立的集合
            fatherChildren.add(comment);

            // 容易忽略的地方：将相对底层的子级放入新建立的集合之后
            // 则表示解除了嵌套关系，对应的其父级的子级应该设为空
            comment.setReply(new ArrayList<>());
        }
    }

    //递归找到该评论的所有父评论
    public void findParents(Comment comment){
        Comment parentComment=commentDao.findParentByCommentId(comment.getCommentid());
        if (parentComment == null) {
            return;
        }
        parentsList.add(parentComment);
        findParents(parentComment);
    }
}
